package zigzag.designpattern.iterator;

public class DvdList {  
	   private String[] titles;  
	   private int titleCount;
	   private int arraySize;
	    
	   public DvdList() {
	        titles = new String[3];    
	        titleCount = 0;
	        arraySize = 3;
	   }    
	   
	   public int count() {
	       return titleCount;
	   }
	   
	   public void append(String titleIn) {
	       if (titleCount >= arraySize) {
	           String[] tempArray = new String[arraySize];
	           for (int i = 0; i < arraySize; i++){
	        	   tempArray[i] = titles[i];
	           }
	           titles = null;
	           arraySize = arraySize + 3;           
	           titles = new String[arraySize];
	           for (int i = 0; i < (arraySize - 3); i++) {
	               titles[i] = tempArray[i];
	           }
	       }
	       titles[titleCount++] = titleIn; 
	   }
	   
	   public void delete(String titleIn) {
	       boolean found = false;
	       for (int i = 0; i < (titleCount -1); i++) {
	           if (found == false) {
	               if (titles[i].equals(titleIn)) {
	                   found = true;
	                   titles[i] = titles[i + 1];
	               }
	           } else {
	               if (i < (titleCount -1)) {
	                   titles[i] = titles[i + 1];
	               } else {
	                   titles[i] = null;
	               }
	           }
	       }
	       
	       if (found == true) {
	           --titleCount;
	       }
	   }
	   
	   
	   public DvdListIterator createIterator() {
	       return new InnerIterator();
	   }
	   
	  
	   private class InnerIterator implements DvdListIterator {
	       private int currentPosition = 0;
	       
	       private InnerIterator() {}
	       
	       public void first() {
	           currentPosition = 0;
	       }
	       
	       public void next() {
	           if (currentPosition < (titleCount)) {
	               ++currentPosition;
	           }
	       }
	       
	       public boolean isDone() {
	           if (currentPosition >= (titleCount)) {
	               return true;
	           } else {
	               return false;
	           }
	       }
	       
	       public String currentItem() {
	           return titles[currentPosition];
	       }
	   }
	}